package com.action.mvc.auth.controller;

import com.action.mvc.auth.common.AjaxResult;
import com.action.mvc.auth.common.BaseEntity;
import com.action.mvc.auth.dto.Authorize;
import com.action.mvc.auth.entity.Role;
import com.action.mvc.auth.entity.User;
import com.action.mvc.auth.entity.UserRole;
import com.action.mvc.auth.service.RoleService;
import com.action.mvc.auth.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * Created by Wuxiang on 2017/4/24.
 */
@Controller
@RequestMapping("/authorize")
public class UserAuthorizeController {
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;

    @RequestMapping("/index")
    public String index(){
        return "authorize_list";
    }

    @RequestMapping("/userRole")
    public String authorizeIndex(){
        return "user_role_list";
    }

    /**
     * 查询权限信息
     * @param page
     * @param size
     * @return 权限集合
     */
    @RequestMapping(value = "/getAuthorizes",method = RequestMethod.POST)
    @ResponseBody
    public List<Authorize> getAuthorizes(int page, int size)
    {
        List<UserRole> userRoles = userService.getUserRoles(page, size);
        Collection<Long> userIds = new HashSet<>();
        Collection<Long> roleIds = new HashSet<>();
        userRoles.forEach(userRole -> {
            userIds.add(userRole.getUserId());
            roleIds.add(userRole.getRoleId());
        });


        List<User> users =  userService.getUsers(userIds);
        List<Role> roles = roleService.getRoles(roleIds);

        Map<Long,User> userMap = BaseEntity.idEntityMap(users);
        Map<Long,Role> roleMap = BaseEntity.idEntityMap(roles);

        List<Authorize> authorizes = new LinkedList<>();
        userRoles.forEach(userRole -> {
            Authorize authorize = new Authorize();
            authorize.setRoleId(userRole.getRoleId());
            authorize.setUserId(userRole.getUserId());
            authorize.setUserName(userMap.get(userRole.getUserId()).getName());
            authorize.setUserRoleId(userRole.getId());
            authorize.setRoleName(roleMap.get(userRole.getRoleId()).getName());
            authorizes.add(authorize);
        });
        return authorizes;
    }

    /**
     * 根据用户id查询用户和角色的 对应关系
     * @param userId
     * @return
     */
    @RequestMapping(value = "/getUserRoleByUserId",method = RequestMethod.POST)
    @ResponseBody
    public List<UserRole> getUserRoleByUserId(Long userId){
        return userService.getUserRoleByUserId(userId);
    }

    /**
     * 设置权限
     * @param user 用户
     * @param roleIds 角色集合
     * @return 操作结果
     */
    @RequestMapping(value = "/setAuthorize",method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult setAuthorize(User user, String roleIds)
    {
        String[] temp = roleIds.split(",");
        Long[] roleIdArray  = new Long[temp.length];
        for (int i = 0; i < roleIdArray.length; i++) {
            roleIdArray[i] = Long.valueOf(temp[i]);
        }
        userService.addUserRoles(user.getId(), roleIdArray);
        return AjaxResult.success();
    }

}
